﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Common
{
    /// <summary>
    /// 游戏屏幕
    /// </summary>
    /// 
    public static class GameScreen
    {
        /// <summary>
        /// 左上方位置【X,Y】,相对一个地图单元大小，非像素
        /// </summary>
        public static Point TopLeftPosition;
        /// <summary>
        /// 右下方位置【X,Y】,相对一个地图单元大小，非像素
        /// </summary>
        public static Point BottomRightPosition;

        /// <summary>
        /// //视窗在地图左边缘X轴的位置（像素），离得越远负值越大
        /// </summary>
        public static int LeftEdge;

        /// <summary>
        /// //视窗在地图上边缘Y轴的位置（像素），离得越远负值越大
        /// </summary>
        public static int TopEdge;

        /// <summary>
        /// Viewport 用于显示窗口大小的像素
        /// </summary>
        public static Point ViewportSize;
        public static void Update(GameTime gameTime, GraphicsDeviceManager graphics, Game game)
        {
            ViewportSize.X = game.GraphicsDevice.Viewport.Width;
            ViewportSize.Y = game.GraphicsDevice.Viewport.Height;
            KeyboardState kState = Keyboard.GetState(); //Gets the current keyboard state.
            //切换全屏模式或窗口模式
            if (kState.IsKeyDown(Keys.Space))
            {
                // The GraphicsDeviceManager class has a built in function called
                // ToggleFullScreen that will handle
                graphics.ToggleFullScreen();
            }
        }
        /// <summary>
        /// 更新屏幕地图坐标位置
        /// </summary>
        /// <param name="mapunitwidth"></param>
        /// <param name="mapunitheigth"></param>
        /// <param name="viewport"></param>
        public static void ResetScreenEdge(int mapunitwidth, int mapunitheigth, Viewport viewport)
        {
            TopLeftPosition.X = -LeftEdge / mapunitwidth;
            TopLeftPosition.Y = -TopEdge / mapunitheigth;
            BottomRightPosition.X = (viewport.Width - LeftEdge) / mapunitwidth;
            BottomRightPosition.Y = (viewport.Height - TopEdge) / mapunitheigth;
        }
        /// <summary>
        /// 位置是否在屏幕中
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public static bool IsPositionInScreen(Point position)
        {
            return ((((position.X >= TopLeftPosition.X) && (position.Y >= TopLeftPosition.Y)) && (position.X <= BottomRightPosition.X)) && (position.Y <= BottomRightPosition.Y));
        }

        public static void AdjustRectangleInViewport(ref Rectangle rect)
        {
            if (rect.Left < 0)
            {
                rect.X += rect.Width;
            }
            if (rect.Top < 0)
            {
                rect.Y += rect.Height;
            }
        }
    }
}
